﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CodeFound
{
    public struct userModel
    {
        public string name;
        public string pwd;
    }

    public delegate void GetModelInstance<T>(T t);
    public delegate void RenderMethod(IAction action, object obj);
    public class ViewTemplateAttribute:Attribute
    {
        public ViewTemplateAttribute()
        {
        }
        public ViewTemplateAttribute(string url)
        {
        }
    }



    public interface IAction
    {
        void Write(string obj);
    }
    [ViewTemplate]
    public static class ViewModel
    {
        public static string url;
        public static void Render(this IAction action, object t)
        {
            userModel model = (userModel)t;
            model.name = "";
            model.pwd = "";
            string name = model.name;
            string pwd = model.pwd;
            ///
            action.Write("");
        }
    }
    public static class ViewModelExtend
    {
        public static Dictionary<string, RenderMethod> dic = null;
        public static void Init()
        {
            dic = new Dictionary<string, CodeFound.RenderMethod>();
            var man= AppDomain.CurrentDomain.GetAssemblies().SelectMany(x => x.DefinedTypes);
            foreach(var n in man)
            {
                string key= n.GetField("url").GetValue(null).ToString();
                RenderMethod method = (RenderMethod)n.GetMethod("Render").CreateDelegate(typeof(RenderMethod));
                dic.Add(key, method);
            }
        }
        public static void Render(this IAction action,string url,object t)
        {
            Dictionary<string, RenderMethod> dic = new Dictionary<string, RenderMethod>();
            if (dic.ContainsKey(url))
            {
                dic[url](action, t);
            }
            else
            {
                throw new KeyNotFoundException();
            }
        }
    }
    public class VController : IAction
    {
        public void Write(string obj)
        { }
    }
    public partial class IndexAction:VController
    {
        public readonly static string actionUrl="regist/";
        public static void Execute(IDictionary<string, object> env)
        {
            IndexAction action = new CodeFound.IndexAction();
            //if (!action.EnvFilter(env))
            //{
            //    return;
            //}
            //if (!action.EnvFilter(Request))
            //{
            //    return;
            //}
            //string name = "";// request.get["name"].AsVar();
            //string pwd = "";// request.get["pwd"].AsVar();
            ////由生成器生成
            //Response response= action.Index(name, pwd);
            //responseFilter(response);
        }
        //由生成器生成
        public string GetUrl(string name, string pwd)
        {
            return $"{actionUrl}{name}-{pwd}{actionUrl.Length.ToString("##")}.html";   
        }
        public void Index(string name,string pwd)
        {
            this.Render("url", new userModel() {
                name=name,
                pwd=pwd
            });
        }
    }
    //public partial class IndexAction : VController
    //{
    //    public bool EnvFilter(IDictionary<string, object> env)
    //    {
    //        return true;
    //    }
    //    public bool RequestFilter(Request request)
    //    {
    //        return true;
    //    }
    //    public bool ResponseFilter(Response response)
    //    {
    //        return true;
    //    }
    //}

}
